package cn.cdtu.tibetplan.service.impl;

import java.util.List;

import cn.cdtu.tibetplan.mapper.DynamicsMapper;
import cn.cdtu.tibetplan.pojo.Dynamics;
import cn.cdtu.tibetplan.pojo.DynamicsExample;
import cn.cdtu.tibetplan.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import cn.cdtu.tibetplan.pojo.DynamicsExample.Criteria;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.cdtu.tibetplan.service.DynamicsService;

import org.springframework.stereotype.Service;

/**
 * 服务实现层
 *
 * @author Administrator
 */
@Service
public class DynamicsServiceImpl implements DynamicsService {

    @Autowired
    private DynamicsMapper dynamicsMapper;

    /**
     * 查询全部
     */
    @Override
    public List<Dynamics> findAll() {
        return dynamicsMapper.selectByExample(null);
    }

    /**
     * 按分页查询
     */
    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Dynamics> page = (Page<Dynamics>) dynamicsMapper.selectByExample(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 增加
     */
    @Override
    public void add(Dynamics dynamics) {
        dynamicsMapper.insert(dynamics);
    }

    /**
     * 修改
     */
    @Override
    public void update(Dynamics dynamics) {
        dynamicsMapper.updateByPrimaryKeySelective(dynamics);
    }

    /**
     * 根据ID获取实体
     *
     * @param id
     * @return
     */
    @Override
    public Dynamics findOne(Long id) {
        return dynamicsMapper.selectByPrimaryKey(id.intValue());
    }

    /**
     * 批量删除
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            dynamicsMapper.deleteByPrimaryKey(id.intValue());
        }
    }

    @Override
    public PageResult findPage(Dynamics dynamics, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        DynamicsExample example = new DynamicsExample();
        Criteria criteria = example.createCriteria();
        if (dynamics != null) {
            if (dynamics.getTitle() != null && dynamics.getTitle().length() > 0) {
                criteria.andTitleLike("%" + dynamics.getTitle() + "%");
            }
            if (dynamics.getInfo() != null && dynamics.getInfo().length() > 0) {
                criteria.andInfoLike("%" + dynamics.getInfo() + "%");
            }
            if (dynamics.getSchoolname() != null && dynamics.getSchoolname().length() > 0) {
                criteria.andSchoolnameLike("%" + dynamics.getSchoolname() + "%");
            }
            if (dynamics.getCreatetime() != null && dynamics.getCreatetime().length() > 0) {
                criteria.andCreatetimeLike("%" + dynamics.getCreatetime() + "%");
            }
            if (dynamics.getUpdatetime() != null && dynamics.getUpdatetime().length() > 0) {
                criteria.andUpdatetimeLike("%" + dynamics.getUpdatetime() + "%");
            }
            if (dynamics.getOperator() != null && dynamics.getOperator().length() > 0) {
                criteria.andOperatorLike("%" + dynamics.getOperator() + "%");
            }
            if (dynamics.getOther1() != null && dynamics.getOther1().length() > 0) {
                criteria.andOther1Like("%" + dynamics.getOther1() + "%");
            }
            if (dynamics.getOther2() != null && dynamics.getOther2().length() > 0) {
                criteria.andOther2Like("%" + dynamics.getOther2() + "%");
            }
            if (dynamics.getOther3() != null && dynamics.getOther3().length() > 0) {
                criteria.andOther3Like("%" + dynamics.getOther3() + "%");
            }

        }
        Page<Dynamics> page = (Page<Dynamics>) dynamicsMapper.selectByExample(example);
        return new PageResult(page.getTotal(), page.getResult());
    }

}
